﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SubSonic;
using BookShop;

public partial class InventoryFilter : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //HiddenField1.Value = AllocationAccess.GenerateID();
            HiddenField1.Value = FormAccess.GenerateID("AllocationDetail", "AllocationID", "BH01");
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string allocationId, isbn, max, stock, storeId;
        allocationId = HiddenField1.Value;
        isbn = GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text;
        max = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
        stock = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
        storeId = DropDownList1.SelectedValue;
        if (AllocationController.CheckStock(allocationId, isbn, storeId, max, stock, cbAmout.Checked))
        {
            GridView2.DataBind();
            this.UpdatePanel1.Update();
        }
        else
        {
            String scriptString = "<script language=javascript>";
            scriptString += "alert('订货量超过库存量,请重新设置！')";
            scriptString += "</script>";
            ClientScriptManager cs = Page.ClientScript;
            //if (!cs.IsStartupScriptRegistered("Startup"))
                cs.RegisterClientScriptBlock(Page.GetType(),"",scriptString);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet ds = new Select(Aggregate.GroupBy((AllocationDetail.StoreIDColumn))).From<AllocationDetail>().Where(AllocationDetail.AllocationIDColumn).IsEqualTo(HiddenField1.Value).ExecuteDataSet();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            new AllocationController().Insert(HiddenField1.Value, DateTime.Now, null, String.Empty, null, "new", ds.Tables[0].Rows[i][0].ToString(), String.Empty);
        }
        
        new FlowProcess().FlowInitalize("2", HiddenField1.Value, Profile.UserName);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //SqlQuery query = new Select().From<OrderPrediction4Store>();

        string sqlstr, str1 = "", str2 = "";
        
        try
        {
            sqlstr = "select * from OrderPrediction4HQ where ";
            if (cbIsLowerThanMin.Checked == true)
            {
                str1 += " or Amount < MinStock";
                GridView1.DataSourceID = "";
            }
            if (cbClassA.Checked == true)
            {
                str1 += " or Classification = 'A'";
                GridView1.DataSourceID = "";
            }
            if (cbClassB.Checked == true)
            {
                str1 += " or Classification = 'B'";
                GridView1.DataSourceID = "";
            }
            if (cbClassC.Checked == true)
            {
                str1 += " or Classification = 'C'";
                GridView1.DataSourceID = "";
            }
            if (txtMin.Text != "" && txtMax.Text != "")
            {
                str1 += " or Amount between " + txtMin.Text + " and " + txtMax.Text;
                GridView1.DataSourceID = "";
            }

            if (LastMonth.Text != "")
            {
                str2 += " or LastMonthSale > " + LastMonth.Text;
                GridView1.DataSourceID = "";
            }
            if (Growth.Text != "")
            {
                str2 += " or (Forecast1 - LastMonthSale)/LastMonthSale >" + Convert.ToString(Convert.ToInt32(Growth.Text) / 100);
                GridView1.DataSourceID = "";
            }

            if (str1 != "" && str2 != "")
            {
                sqlstr += "(ISBN = ''" + str1 + ") and (ISBN = ''" + str2 + ")";
            }
            else if (str1 == "" && str2 == "")
            {
                sqlstr += "ISBN != ''";
            }
            else
            {
                sqlstr += "ISBN = ''" + str1 + str2;
            }

            if (GridView1.DataSourceID == "")
            {
                sqlstr += " order by LastMonthSale DESC";
                
                GridView1.DataSource = FormAccess.GetFilteredSet(sqlstr);
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return;
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string store_id = DropDownList1.SelectedValue;
        GridView1.DataSourceID = "";
        GridView1.DataSource = new Select().From<OrderPrediction4Store>()
            .Where(OrderPrediction4Store.Columns.StoreID).IsEqualTo(store_id)
            .OrderDesc(OrderPrediction4Store.Columns.LastMonthSale)
            .ExecuteDataSet();
        GridView1.DataBind();
    }
}
